package com.ktjy.iottemp.rabbitmq.config;

import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {

    // 声明队列
    @Bean
    public Queue emqxTempQueue() {
        // 参数分别为：队列名、是否持久化、是否排他、是否自动删除、其他属性
        return new Queue("emqx_temp_queue", true, false, false, null);
    }

    // 如果需要手动确认消息，配置此工厂
    @Bean
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        // 设置为手动确认模式
        //factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        // 可以配置并发消费者数量
        factory.setConcurrentConsumers(1);
        factory.setMaxConcurrentConsumers(5);
        return factory;
    }
}